package cn.iocoder.yudao.module.xiaoBizHandler.service.codegen.newInner.yudao.template.java.dal;

import cn.hutool.core.text.CharSequenceUtil;
import cn.iocoder.yudao.framework.common.genCode.common.StringBuilderHandler;
import cn.iocoder.yudao.module.xiaoBizHandler.dal.dataobject.codegen.XiaoBizHandlerCodegenColumnDO;
import cn.iocoder.yudao.module.xiaoBizHandler.dal.dataobject.codegen.XiaoBizHandlerCodegenTableDO;
import cn.iocoder.yudao.module.xiaoBizHandler.service.codegen.newInner.yudao.handler.CommonColumn;
import cn.iocoder.yudao.module.xiaoBizHandler.service.codegen.newInner.yudao.handler.PathHandler;
import cn.iocoder.yudao.module.xiaoBizHandler.service.codegen.newInner.yudao.template.CodeGenTemplate;
import cn.iocoder.yudao.module.xiaoBizHandler.service.codegen.newInner.GlobalBinding;
import cn.iocoder.yudao.module.xiaoBizHandler.service.codegen.newInner.LocalBinding;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;


@Component
public class Mapper implements CodeGenTemplate {

    @Autowired
    CommonColumn commonColumn;

    public String getPath(XiaoBizHandlerCodegenTableDO table, List<XiaoBizHandlerCodegenColumnDO> columns, GlobalBinding globalBinding, LocalBinding localBinding, XiaoBizHandlerCodegenColumnDO primaryColumn) {
        PathHandler pathHandler = PathHandler.builder()
                .table(table)
                .columns(columns)
                .globalBinding(globalBinding)
                .localBinding(localBinding)
                .primaryColumn(primaryColumn)
                .build();
        return pathHandler.javaModuleImplMainFilePath("dal/mysql/" + table.getBusinessName() + "/" + table.getClassName() + "Mapper");
    }


    public String render(XiaoBizHandlerCodegenTableDO table, List<XiaoBizHandlerCodegenColumnDO> columns, GlobalBinding globalBinding, LocalBinding localBinding, XiaoBizHandlerCodegenColumnDO primaryColumn) {
        StringBuilderHandler code = new StringBuilderHandler();

        code.ap("")
                .apAndBl("package " + globalBinding.getBasePackage() + ".module." + table.getModuleName() + ".dal.mysql." + table.getBusinessName() + ";\n")
                .apAndBl("import java.util.*;")
                .bl()
                .apAndBl("import " + globalBinding.getPageResultClassName() + ";")
                .apAndBl("import " + globalBinding.getQueryWrapperClassName() + ";")
                .apAndBl("import " + globalBinding.getBaseMapperClassName() + ";")
                .apAndBl("import " + globalBinding.getBasePackage() + ".module." + table.getModuleName() + ".dal.dataobject." + table.getBusinessName() + "." + table.getClassName() + "DO;")
                .apAndBl("import org.apache.ibatis.annotations.Mapper;")
                .apAndBl("import " + globalBinding.getBasePackage() + ".module." + table.getModuleName() + ".controller." + localBinding.getSceneEnum().getBasePackage() + "." + table.getBusinessName() + ".vo.*;")
                .apAndBl("/**")
                .apAndBl(" * " + table.getClassComment() + " Mapper")
                .apAndBl(" *")
                .apAndBl(" * @author " + table.getAuthor() + "")
                .apAndBl(" */")
                .apAndBl("@Mapper")
                .apAndBl("public interface " + table.getClassName() + "Mapper extends BaseMapperX<" + table.getClassName() + "DO> {")
                .apAndBl("")
                .apAndBl("    default PageResult<" + table.getClassName() + "DO> selectPage(" + localBinding.getSceneEnum().getPrefixClass() + "" + table.getClassName() + "PageReqVO reqVO) {")
                .apAndBl("        return selectPage(reqVO, new LambdaQueryWrapperX<" + table.getClassName() + "DO>()")
                .apAndBl("			" + getListCondition(table, columns, globalBinding, localBinding))
                // 大多数情况下，id 倒序
                .apAndBl("                .orderByDesc(" + table.getClassName() + "DO::getId));")
                .bl()
                .apAndBl("    }")
                .bl()
                .apAndBl("    default List<" + table.getClassName() + "DO> selectList(" + localBinding.getSceneEnum().getPrefixClass() + "" + table.getClassName() + "ExportReqVO reqVO) {")
                .apAndBl("        return selectList(new LambdaQueryWrapperX<" + table.getClassName() + "DO>()")
                .apAndBl("			" + getListCondition(table, columns, globalBinding, localBinding))
                // 大多数情况下，id 倒序
                .apAndBl("                .orderByDesc(" + table.getClassName() + "DO::getId));")
                .bl()
                .apAndBl("    }")
                .bl()
                .apAndBl("}")

                .apAndBl("")
        ;

        return code.toString();
    }

    private String getListCondition(XiaoBizHandlerCodegenTableDO table, List<XiaoBizHandlerCodegenColumnDO> columns, GlobalBinding globalBinding, LocalBinding localBinding) {
        StringBuilderHandler code = new StringBuilderHandler();
        for (XiaoBizHandlerCodegenColumnDO column : columns) {
            if (commonColumn.isCommonColumn(column.getColumnName()))    continue;
            String javaField = CharSequenceUtil.upperFirst(column.getJavaField());
            if (column.getListOperation()) {
                String listOperationCondition = column.getListOperationCondition();
                switch (listOperationCondition) {
                    case "=":
                        code.apAndBl("                .eqIfPresent(" + table.getClassName() + "DO::get" + javaField + ", reqVO.get" + javaField + "())");
                        break;
                    case "!=":
                        code.apAndBl("                .neIfPresent(" + table.getClassName() + "DO::get" + javaField + ", reqVO.get" + javaField + "())");
                        break;
                    case ">":
                        code.apAndBl("                .gtIfPresent(" + table.getClassName() + "DO::get" + javaField + ", reqVO.get" + javaField + "())");
                        break;
                    case ">=":
                        code.apAndBl("                .geIfPresent(" + table.getClassName() + "DO::get" + javaField + ", reqVO.get" + javaField + "())");
                        break;
                    case "<":
                        code.apAndBl("                .ltIfPresent(" + table.getClassName() + "DO::get" + javaField + ", reqVO.get" + javaField + "())");
                        break;
                    case "<=":
                        code.apAndBl("                .leIfPresent(" + table.getClassName() + "DO::get" + javaField + ", reqVO.get" + javaField + "())");
                        break;
                    case "LIKE":
                        code.apAndBl("                .likeIfPresent(" + table.getClassName() + "DO::get" + javaField + ", reqVO.get" + javaField + "())");
                        break;
                    case "BETWEEN":
                        code.apAndBl("                .betweenIfPresent(" + table.getClassName() + "DO::get" + javaField + ", reqVO.getBegin" + javaField + "(), reqVO.getEnd" + javaField + "())");
                        break;
                    default:
                        break;
                }
            }
        }
        return code.toString();
    }

}
